package org.zp.service;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zp.mapper.RMMapper;
import org.zp.pojo.RM;

@Service
@Transactional(rollbackFor = Exception.class)//出现异常就回滚事务
public class RMServiceimpl implements RMService{
@Resource
private RMMapper r;

@Override
public Integer saveAccess(Integer rid, String[] arr) {
	//删除之前的选择的选项
	Integer flag = 0;
	System.out.println("我们选的复选框数:"+arr.length);
	System.out.println("该角色的rid"+rid);
	List<RM>list = r.getRMlist(rid);
	System.out.println("原本的复选框数:"+list.size());
	if (list.size()>0) {
		Integer delete = r.deleteAccess(rid);
		System.out.println("delete:"+delete);
		if (delete <= 0) {
			System.out.println("删除失败");
			flag = 0;
			return  flag;
		}else {
			flag = 1;
			System.out.println("删除成功");
		}
	}
	
	//将对应得选项遍历添加
	int option = 0;
	String str = null;
	
	int count = 0;
	for(int i=0;i<arr.length;i++) {
		str = arr[i];
		option = Integer.parseInt(str);
		Integer add = r.insert(rid,option);
		if(add < 0) {
			System.out.println("添加失败");
			flag = 0;
			return  flag;
		}else {
			flag = 1;
			count++;
			System.out.println("添加成功");
			
		}
	}
	
	System.out.println("复选框添加数为:"+count);
	return flag;

}
}
